<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary" @click="dialogVisible=true">增加停车计费规则</el-button>
      <el-button>导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上线(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式">
          <template #default="scope">
            {{ formatChargeType(scope.row.chargeType) }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" />
        <el-table-column label="操作" fixed="right" width="120">
          <template #default="scope">
            <el-button size="mini" type="text" @click="editRules(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delRules(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :current-page="params.page"
        :page-size="params.pageSize"
        @current-change="handleCurrentChange"
      />
    </div>
    <addCarRules ref="editForm" :current-id="currentId" :dialog-visible.sync="dialogVisible" @addCarRule="getRuleList" />
  </div>
</template>

<script>
import { deleteCarRuleAPI, getCarRuleAPI } from '@/api/carRule'
import { utils, writeFileXLSX } from 'xlsx'
import addCarRules from './components/addCarRules.vue'
export default {
  name: 'Building',
  components: { addCarRules },
  data() {
    return {
      ruleList: [],
      params: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      currentId: null
    }
  },
  created() {
    this.getRuleList()
  },
  methods: {
    async getRuleList() {
      const res = await getCarRuleAPI({
        page: this.params.page,
        pageSize: this.params.pageSize
      })
      console.log('规则', res)
      this.ruleList = res.data.rows
      this.total = res.data.total
    },
    handleCurrentChange(value) {
      this.params.page = value
      this.getRuleList()
    },
    async exportExcel() {
      // 获取要导出的业务数据
      const res = await getCarRuleAPI(this.params)
      // 表头英文字段key
      const tableKeys = ['ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
      // 表头中文字段value
      const tableValues = ['计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
      // 以excel表格的顺序调整后端数据
      const sheetData = res.data.rows.map(item => {
        const obj = {}
        tableKeys.forEach((key, index) => {
          // 针对计费规则做处理
          if (key === 'chargeType') {
            obj[key] = this.formatChargeType(item[key])
          } else {
            obj[key] = item[key]
          }
        })
        return obj
      })
      // 创建一个工作表
      const ws = utils.json_to_sheet(sheetData)
      // 创建一个新的工作簿
      const wb = utils.book_new()
      // 将工作表添加到工作簿中
      utils.book_append_sheet(wb, ws, 'Data')
      // 改写表头、
      utils.sheet_add_aoa(ws, [tableValues], { origin: 'A1' })
      writeFileXLSX(wb, '计费规则.xlsx')
    },
    // 计费规则
    formatChargeType(type) {
      const TYPEMAP = {
        'duration': '按时长计费',
        'turn': '按次计费',
        'partition': '分段收费'
      }
      return TYPEMAP[type]
    },
    // 删除
    delRules(id) {
      this.$confirm('确定要删除吗').then(async() => {
        await deleteCarRuleAPI(id)
        this.$message.success('删除成功')
        this.getRuleList()
      })
    },
    editRules(id) {
      this.dialogVisible = true
      this.currentId = id
      console.log(this.currentId)
      this.$nextTick(() => {
        this.$refs.editForm.getCarRuleDetail()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
